package com.dykj.fireFighting.web.controller.government;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dykj.fireFighting.api.base.fireEngine.entity.FireEngine;
import com.dykj.fireFighting.api.base.government.entity.GovernmentProcess;
import com.dykj.fireFighting.api.base.government.entity.GovernmentProject;
import com.dykj.fireFighting.api.base.government.entity.GovernmentProjectView;
import com.dykj.fireFighting.api.base.government.service.*;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@Slf4j
@Api(value = "精准治理-专项治理-接口", tags = {"精准治理-专项治理接口"})
@RestController
@RequestMapping("/GovernmentProject")
public class GovernmentProjectController {
    /**
     * 精准治理-专项治理列表
     *
     * @return
     */
    @DubboReference
    private IGovProFjService govProFjService;
    @DubboReference
    private IGovProOtherService govProOtherService;
    @DubboReference
    private IGovListApiService govListApiService;
    @DubboReference
    private IGovernmentListService governmentListService;
    @DubboReference
    private IGovernmentProjectViewService governmentProjectViewService;
    @DubboReference
    private IGovernmentProcessService governmentProcessService;
    @DubboReference
    private IGovernmentProjectService governmentProjectService;
    @AutoLog(value = "精准治理-专项治理-分页列表查询")
    @ApiOperation(value = "精准治理-专项治理分页列表", notes = "精准治理-专项治理分页列表")
    @GetMapping("/list")
    public CommonResult treeGrid(@RequestParam(value = "page",required = false)Integer page,
                                                @RequestParam(value = "limit",required = false)Integer limit,
                                String pName,String beginTime, String endTime) {
        return CommonResult.success(governmentProjectViewService.getPageList(pName,beginTime,endTime,page,limit));
    }
    @PostMapping("/delbyid")
    public CommonResult delbyid(@RequestBody String[] ids) {
        try {
            governmentProjectService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
    @PostMapping("/save")
    public CommonResult saveorup(@Valid @RequestBody GovernmentProject governmentProject) {
        String id=governmentProject.getId();
        LoginUserVo u = ShiroUtil.getUser();
        if(StringUtils.equals("0",id)){
            governmentProject.setId(IdUtil.simpleUUID());
            governmentProject.setCenterId(u.getEnterpriseId()+"");
            governmentProject.setCreateBy(u.getId()+"");
            governmentProject.setCreateTime(DateUtil.date());
        }
        governmentProject.setUpdateBy(u.getId()+"");
        if(StringUtils.isNotBlank(governmentProject.getCg())){
            String cg=governmentProject.getCg().replaceAll("#","\"");
            List<GovernmentProcess> list= JSONObject.parseArray(cg, GovernmentProcess.class);
            for(GovernmentProcess governmentProcess:list){
                if(StringUtils.isBlank(governmentProcess.getId())){
                    governmentProcess.setId(IdUtil.simpleUUID());
                }
                if(StringUtils.isBlank(governmentProcess.getPid())){
                    governmentProcess.setPid(governmentProject.getId());
                }

            }
            governmentProcessService.sb(list);
            governmentProject.setCg(JSON.toJSONString(list).replaceAll("\"","#"));
        }


        governmentProjectService.saveOrUpdate(governmentProject);
        return CommonResult.success("操作成功！");
    }
    @AutoLog(value = "消防车-获取消防车信息")
    @ApiOperation(value = "获取消防车信息", notes = "获取消防车信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/getbyid/{id}")
    public CommonResult<GovernmentProject> info(@PathVariable("id") String id) {
        GovernmentProject governmentProject=governmentProjectService.getById(id);
        governmentProject.setCg(JSON.toJSONString(governmentProcessService.getListByPid(id)).replaceAll("\"","#"));
        return CommonResult.success(governmentProject);
    }
    @AutoLog(value = "消防车-获取消防车信息")
    @GetMapping("/getall")
    public CommonResult<Object> info() {
        return CommonResult.success(governmentProjectViewService.list());
    }
    @AutoLog(value = "精准治理-大数据页面接口")
    @ApiOperation(value = "获取精准治理接口", notes = "获取精准治理")
    @GetMapping("/getBigData")
    public CommonResult<Map<String,Object>> getBigData() {
        LinkedHashMap<String,Object> reMapped = new LinkedHashMap<>();
        reMapped.put("l1",govProFjService.list());
        reMapped.put("l2",govProOtherService.list());
        GovernmentProjectView governmentProjectViewLast=governmentProjectViewService.getLast();
        reMapped.put("c1",governmentProjectViewLast);
        reMapped.put("c2",governmentProjectViewLast.getZlgc());
        reMapped.put("r2",govListApiService.getByPSid(null,null));

        reMapped.put("c1Opt",governmentProjectService.getGovProOpt());
        reMapped.put("r2Opt",govListApiService.getGovListOpt(null));
        return CommonResult.success(reMapped);
    }
    @AutoLog(value = "精准治理-大数据页面接口")
    @ApiOperation(value = "获取精准治理接口", notes = "获取精准治理")
    @GetMapping("/c1Choose")
    public CommonResult<Map<String,Object>> c1Choose(String cid) {
        LinkedHashMap<String,Object> reMapped = new LinkedHashMap<>();
        GovernmentProjectView governmentProjectViewLast=new GovernmentProjectView();

        if(StringUtils.isNotBlank(cid)){
            governmentProjectViewLast=governmentProjectViewService.getById(cid);

        }else{
            governmentProjectViewLast=governmentProjectViewService.getLast();
        }

        reMapped.put("c1",governmentProjectViewLast);
        reMapped.put("c2",governmentProjectViewLast.getZlgc());
        reMapped.put("c3",governmentProcessService.getXmsj(governmentProjectViewLast.getId()));
        reMapped.put("r2",govListApiService.getByPSid(cid,null));
        reMapped.put("c1Opt",governmentProjectService.getGovProOpt());
        reMapped.put("r2Opt",govListApiService.getGovListOpt(cid));
        return CommonResult.success(reMapped);

    }
    @AutoLog(value = "精准治理-大数据页面接口")
    @ApiOperation(value = "获取精准治理接口", notes = "获取精准治理")
    @GetMapping("/r2Choose")
    public CommonResult<Map<String,Object>> r2Choose(String sid) {
        LinkedHashMap<String,Object> reMapped = new LinkedHashMap<>();
        reMapped.put("r2",govListApiService.getByPSid(null,sid));
        return CommonResult.success(reMapped);

    }
   /* @GetMapping("/bdl1")
    public CommonResult<Map<String,Object>> bdl1(String date) {
        LinkedHashMap<String,Object> reMapped = new LinkedHashMap<>();
        reMapped.put("l1",govProFjService.list());
        reMapped.put("l2",govProOtherService.list());
        GovernmentProjectView governmentProjectViewLast=governmentProjectViewService.getLast();
        reMapped.put("c1",governmentProjectViewLast);
        reMapped.put("c2",governmentProjectViewLast.getZlgc());
        reMapped.put("r2",govListApiService.getByPSid(null,null));

        reMapped.put("c1Opt",governmentProjectService.getGovProOpt());
        reMapped.put("r2Opt",govListApiService.getGovListOpt(null));
        return CommonResult.success(reMapped);
    }*/
    @GetMapping("/bdleft")
    public CommonResult<Map<String,Object>> bdleft(String date) {
        String stDate=date.split("_")[0];
        String endDate=date.split("_")[1];
        Map<String,Object> reMapped=new HashMap<>();

        //实时数据
        Map<String,Object> sssjMapped=new HashMap<>();
            //专项治理
            sssjMapped.put("zxzl",governmentProjectService.getCountByDate(stDate,endDate));
            //专项治理
            sssjMapped.put("cydw",governmentProcessService.getCountByDateC7(stDate,endDate));
            //专项治理
            sssjMapped.put("cldw",governmentProcessService.getCountByDateC8(stDate,endDate));
        //实时数据
        reMapped.put("sssj",sssjMapped);

        //治理成果-单位
        reMapped.put("zlcgdw",governmentProcessService.getZlcg(stDate,endDate));
        //治理成果-金额
        reMapped.put("zlcgje",govProFjService.list());
        //手段占比
        reMapped.put("sdzb",govProOtherService.list());
        //单位主导项目统计
        reMapped.put("dwzdxmtj",governmentProjectService.getZddwTj(stDate,endDate));
        return CommonResult.success(reMapped);
    }
}
